#include "dvbinner.h"

typedef struct
{
	int 			VerifyByte;
	dvb_service_t CurrentService;
	int			CurServiceGroupId;
	dvb_service_t LastService;
	int			LastServiceGroupId;
	int			UseGlobalSet;	
	int 			CurVolume;	
	int			CurStereoMode;	
	int			Timezone; 

	//add for new search mechanism
	dvb_tuner_t 	m_stMainFreq;
	unsigned int	m_u32CurrentSource; //0 -t,1 -c
}DVBVIDEOINFO;

DVBVIDEOINFO DvbVideoInfo;
int bCurServiceParaChg = 0;

void DVBSysDBDefaut()
{
	memset(&DvbVideoInfo, 0, sizeof(DvbVideoInfo));
	DvbVideoInfo.CurVolume = 30;
	DvbVideoInfo.CurStereoMode = AUDIO_MODE_LEFT;
	DvbVideoInfo.UseGlobalSet = 1;
	DvbVideoInfo.Timezone = 8;

	memset(&DvbVideoInfo.m_stMainFreq, 0, sizeof(DvbVideoInfo.m_stMainFreq));
	DvbVideoInfo.m_u32CurrentSource = 1;
}

int DVBSysDBSerialize(unsigned char* pu8Data, unsigned int* pu32Len)
{
	int s32Ret = -1;
	
	if(pu8Data && pu32Len && *pu32Len > sizeof(DVBVIDEOINFO))
	{
		s32Ret = 0;
		DvbVideoInfo.VerifyByte = VERIFIED_BYTES;
		memcpy(pu8Data, &DvbVideoInfo, sizeof(DVBVIDEOINFO));
		*pu32Len = sizeof(DVBVIDEOINFO);
		bCurServiceParaChg = 0;
	}
	else
	{
		mwError("DVBSysDBSerialize() - receive error param, pu8Data = %p, pu32Len = %p\r\n", pu8Data, pu32Len);
	}
	
	return s32Ret;
}

int DVBSysDBDeserialize(unsigned char* pu8Data, unsigned int* pu32Len)
{
	int s32Ret = -1;
	
	if(pu8Data && pu32Len && *pu32Len >= sizeof(DVBVIDEOINFO))
	{
		s32Ret = 0;
		memcpy(&DvbVideoInfo, pu8Data, sizeof(DVBVIDEOINFO));
		if(DvbVideoInfo.VerifyByte != VERIFIED_BYTES)
		{
			memset((unsigned char*)&DvbVideoInfo, 0, sizeof(DVBVIDEOINFO));
			DVBSysDBDefaut();
			s32Ret = -1;
		}
	}
	else
	{
		mwError("DVBSysDBSerialize() - receive error param, pu8Data = %p, pu32Len = %p\r\n", pu8Data, pu32Len);
	}
	
	return s32Ret;
}

int DVBSysDBSetCurrentService(dvb_service_t stService)
{
	int s32Ret = -1;
	
	if(stService.frequence != DvbVideoInfo.CurrentService.frequence || stService.netId != DvbVideoInfo.CurrentService.netId
		|| stService.tsId != DvbVideoInfo.CurrentService.netId || stService.serviceId != DvbVideoInfo.CurrentService.serviceId)
	{
		DvbVideoInfo.CurrentService = stService;
		s32Ret = 0;
		bCurServiceParaChg = 1;
	}

	return s32Ret;
}

int DVBSysDBGetCurrentService(dvb_service_t* pstService)
{
	int s32Ret = -1;
	
	if(pstService)
	{
		memcpy(pstService, &DvbVideoInfo.CurrentService, sizeof(dvb_service_t));
		s32Ret = 0;
	}

	return s32Ret;
}

int DVBSysDBSetLastService(dvb_service_t stService)
{
	int s32Ret = -1;
	
	if(stService.frequence != DvbVideoInfo.LastService.frequence || stService.netId != DvbVideoInfo.LastService.netId
		|| stService.tsId != DvbVideoInfo.LastService.netId || stService.serviceId != DvbVideoInfo.LastService.serviceId)
	{
		DvbVideoInfo.LastService = stService;
		s32Ret = 0;
		bCurServiceParaChg = 1;
	}

	return s32Ret;
}

int DVBSysDBGetLastService(dvb_service_t* pstService)
{
	int s32Ret = -1;
	
	if(pstService)
	{
		memcpy(pstService, &DvbVideoInfo.LastService, sizeof(dvb_service_t));
		s32Ret = 0;
	}

	return s32Ret;	
}

int DVBSysDBSetGlobalSet(unsigned int u32Switch)
{
	int s32Ret = -1;
	
	if(u32Switch != DvbVideoInfo.UseGlobalSet)
	{
		DvbVideoInfo.UseGlobalSet = u32Switch;
		s32Ret = 0;
		bCurServiceParaChg = 1;
	}
	
	return s32Ret;
}

int DVBSysDBGetGlobalSet(unsigned int* pu32Switch)
{
	int s32Ret = -1;

	if(pu32Switch)
	{
		*pu32Switch = DvbVideoInfo.UseGlobalSet;
		s32Ret = 0;
	}
	
	return s32Ret;
}

int DVBSysDBSetGlobalVolume(unsigned int u32Volume)
{
	int s32Ret = -1;
	
	if(u32Volume != DvbVideoInfo.CurVolume)
	{
		DvbVideoInfo.CurVolume = u32Volume;
		s32Ret = 0;
		bCurServiceParaChg = 1;
	}
	
	return s32Ret;
}

int DVBSysDBGetGlobalVolume(unsigned int* pu32Volume)
{
	int s32Ret = -1;

	if(pu32Volume)
	{
		*pu32Volume = DvbVideoInfo.CurVolume;
		s32Ret = 0;
	}
	
	return s32Ret;	
}

int DVBSysDBSetGlobalStereoMode(unsigned int u32StereoMode)
{
	int s32Ret = -1;
	
	if(u32StereoMode != DvbVideoInfo.CurStereoMode)
	{
		DvbVideoInfo.CurStereoMode= u32StereoMode;
		s32Ret = 0;
		bCurServiceParaChg = 1;
	}
	
	return s32Ret;	
}

int DVBSysDBGetGlobalStereoMode(unsigned int* pu32StereoMode)
{
	int s32Ret = -1;

	if(pu32StereoMode)
	{
		*pu32StereoMode = DvbVideoInfo.CurStereoMode;
		s32Ret = 0;
	}
	
	return s32Ret;		
}

int DVBSysDBSetTimezone(int s32Timezone)
{
	int s32Ret = -1;
	
	if(s32Timezone != DvbVideoInfo.CurVolume)
	{
		DvbVideoInfo.Timezone= s32Timezone;
		s32Ret = 0;
		bCurServiceParaChg = 1;
	}
	
	return s32Ret;	
}

int DVBSysDBGetTimezone(int* s32Timezone)
{
	int s32Ret = -1;

	if(s32Timezone)
	{
		*s32Timezone = DvbVideoInfo.Timezone;
		s32Ret = 0;
	}
	
	return s32Ret;		
}

int DVBSysDBSetCurrentGroupID(unsigned int u32GroupID)
{
	int s32Ret = -1;
	
	if(u32GroupID != DvbVideoInfo.CurServiceGroupId)
	{
		DvbVideoInfo.CurServiceGroupId = u32GroupID;
		s32Ret = 0;
		bCurServiceParaChg = 1;
	}
	
	return s32Ret;	
}

int DVBSysDBGetCurrentGroupID(unsigned int* pu32GroupID)
{
	int s32Ret = -1;

	if(pu32GroupID)
	{
		*pu32GroupID = DvbVideoInfo.CurServiceGroupId;
		s32Ret = 0;
	}
	
	return s32Ret;		
}

int DVBSysDBSetLastGroupID(unsigned int u32GroupID)
{
	int s32Ret = -1;
	
	if(u32GroupID != DvbVideoInfo.CurServiceGroupId)
	{
		DvbVideoInfo.LastServiceGroupId = u32GroupID;
		s32Ret = 0;
		bCurServiceParaChg = 1;
	}
	
	return s32Ret;	
}

int DVBSysDBGetLastGroupID(unsigned int* pu32GroupID)
{
	int s32Ret = -1;

	if(pu32GroupID)
	{
		*pu32GroupID = DvbVideoInfo.LastServiceGroupId;
		s32Ret = 0;
	}
	
	return s32Ret;		
}

int DVBSysDBSetCurrentSource(unsigned int u32Source)
{
	int s32Ret = -1;
	
	if(u32Source != DvbVideoInfo.m_u32CurrentSource)
	{
		DvbVideoInfo.m_u32CurrentSource = u32Source;
		s32Ret = 0;
		bCurServiceParaChg = 1;
	}
	
	return s32Ret;	
}

int DVBSysDBGetCurrentSource(unsigned int* pu32Source)
{
	int s32Ret = -1;

	if(pu32Source)
	{
		*pu32Source = DvbVideoInfo.m_u32CurrentSource;
		s32Ret = 0;
	}
	
	return s32Ret;		
}

int DVBSysDBSetMainFreq(dvb_tuner_t stFreq)
{
	int s32Ret = -1;

	if(stFreq.bandwidth != DvbVideoInfo.m_stMainFreq.bandwidth || stFreq.frequency != DvbVideoInfo.m_stMainFreq.frequency
		|| stFreq.modulation != DvbVideoInfo.m_stMainFreq.modulation || stFreq.symbol_rate != DvbVideoInfo.m_stMainFreq.symbol_rate
		|| stFreq.tunertype != DvbVideoInfo.m_stMainFreq.tunertype)
	{
		memcpy(&DvbVideoInfo.m_stMainFreq, &stFreq, sizeof(dvb_tuner_t));
		s32Ret = 0;
		bCurServiceParaChg = 1;
	}
	
	return s32Ret;	
}

int DVBSysDBGetMainFreq(dvb_tuner_t* pstFreq)
{
	int s32Ret = -1;

	if(pstFreq)
	{
		memcpy(pstFreq, &DvbVideoInfo.m_stMainFreq, sizeof(dvb_tuner_t));
		s32Ret = 0;
	}
	
	return s32Ret;		
}

int DVBSysDBChanged()
{
	return bCurServiceParaChg;
}

